package com.phb.puhuibao.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.idp.pub.context.AppContext;
import com.idp.pub.dao.IBaseDao;
import com.idp.pub.dao.IPagerDao;
import com.idp.pub.service.impl.DefaultBaseService;
import com.phb.puhuibao.common.Functions;
import com.phb.puhuibao.entity.ItemInvestment;
import com.phb.puhuibao.entity.LoanItem;
import com.phb.puhuibao.entity.MobileUser;
import com.phb.puhuibao.entity.MuserCollection;
import com.phb.puhuibao.entity.UserLoan;
import com.phb.puhuibao.entity.UserMessage;
import com.phb.puhuibao.service.MobileUserService;
import com.phb.puhuibao.service.MuserCollectionService;

@Transactional
@Service("muserCollectionService")
public class MuserCollectionServiceImpl extends DefaultBaseService<MuserCollection, String>
		implements MuserCollectionService {
	@Resource(name = "muserCollectionDao")
	public void setBaseDao(IBaseDao<MuserCollection, String> baseDao) {
		super.setBaseDao(baseDao);
	}

	@Resource(name = "muserCollectionDao")
	public void setPagerDao(IPagerDao<MuserCollection> pagerDao) {
		super.setPagerDao(pagerDao);
	}

	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	@Resource(name = "appContext")
	private AppContext appContext;

	// 我的收藏(0职位收藏1企业收藏)
	@Override
	public List<Map<String, Object>> getMuserCollection(String mUserId, String longt, String lat, String type,String pageno) {
		int mUserId1 = Integer.parseInt(mUserId);
		List<Map<String, Object>> list1 = null;
		double longt1 = 0.00;
		double lat1 = 0.00;
		if (longt != null && !longt.equals("") && lat != null && !lat.equals("")) {
			BigDecimal a = new BigDecimal(longt);
			longt1 = a.setScale(6, BigDecimal.ROUND_DOWN).doubleValue();
			BigDecimal b = new BigDecimal(lat);
			lat1 = b.setScale(6, BigDecimal.ROUND_DOWN).doubleValue();
		} else {
			longt1 = 116.395645; // 默认北京经纬度
			lat1 = 39.929986;
		}
		StringBuffer sb = new StringBuffer("");
		if (!type.equals("") && !type.equals(null)) {
			if (type.equals("0")) {
				sb.append("SELECT  a.job_id,a.m_user_id,a.job_classtype,a.job_title,a.job_type_type,a.job_salary,CASE  WHEN a.salary_unit=1 THEN '元/时' WHEN a.salary_unit=2 THEN '元/日' WHEN a.salary_unit=3 THEN '元/周' WHEN a.salary_unit=4 THEN '元/月' WHEN a.salary_unit=5 THEN '元/次' WHEN a.salary_unit=6 THEN '元/项目' END salaryUnit,a.salary_unit,a.create_time,a.job_end_date,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(("
								+ lat1 + "*PI()/180-lat*PI()/180)/2),2)+COS(" + lat1
								+ "*PI()/180)*COS(lat*PI()/180)*POW(SIN((" + longt1
								+ "*PI()/180-longt*PI()/180)/2),2)))*1000) AS juli,a.`status`,a.job_type FROM phb_muser_job_release a WHERE  a.job_id in (SELECT a.job_id FROM phb_muser_collection a WHERE a.user_id="
								+ mUserId1
								+ " AND a.collection_type = 0) ORDER BY a.`status` DESC,a.create_time DESC, a.job_salary DESC, juli ASC ");
				if (!pageno.equals("") && !pageno.equals(null)) {
					int pageno1 = Integer.parseInt(pageno);
					sb.append(" LIMIT "+pageno1*30+","+(pageno1+1)*30);
				} else {
					sb.append(" LIMIT 0,30");
				}
				String sql = sb.toString();
				List<Map<String, Object>> lsit5 = new ArrayList<Map<String, Object>>();
				list1 = this.jdbcTemplate.queryForList(sql);
				for (Map<String, Object> map : list1) {
					Map<String, Object> params = new HashMap<String, Object>();
					int jobId = (int) map.get("job_id");// 用户id
					int job_classtype = (int) map.get("job_classtype");// 兼职类型
																		// 1.体验2.互动3.分享4.校园5.个人
					List<Map<String, Object>> list2 = null;
					String sql2 = "SELECT a.image,a.job_classtype FROM phb_part_time_job a WHERE a.parttime_job_id="
							+ job_classtype + "";
					list2 = this.jdbcTemplate.queryForList(sql2);
					String jobClasstype = (String) list2.get(0).get("job_classtype");
					String image = (String) list2.get(0).get("image");

					String jobTitle = (String) map.get("job_title");// 标题

					int job_type_type = (int) map.get("job_type_type");// 兼职类型下属分支
					List<Map<String, Object>> list3 = null;
					String sql3 = "SELECT a.job_type_type FROM phb_part_time_job_type a WHERE  a.parttime_job_type_id="
							+ job_type_type + "";
					list3 = this.jdbcTemplate.queryForList(sql3);
					String jobTypeType = (String) list3.get(0).get("job_type_type");

					int jobSalary = (int) map.get("job_salary");// 薪资待遇
					String salaryUnit1 = (String) map.get("salaryUnit");// 薪资单位(如元/日)

					int salary_unit = (int) map.get("salary_unit");// 薪资结算单位(如日结)
					List<Map<String, Object>> list4 = null;
					String sql4 = "SELECT a.settlement_name FROM phb_settlement_method a WHERE a.id=" + salary_unit
							+ "";
					list4 = this.jdbcTemplate.queryForList(sql4);
					String settlementName = (String) list4.get(0).get("settlement_name");

					Date create_time = (Date) map.get("create_time");// 创建时间
					SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
					String createTime = formatter.format(create_time);
					Date job_end_date = (Date) map.get("job_end_date");// 结束时间
					SimpleDateFormat formatter1 = new SimpleDateFormat("MM-dd");
					String jobEndDate = formatter1.format(job_end_date);

					Double juli = (Double) map.get("juli");// 距离
					String distance = "";
					if (juli > 1000.0) {
						BigDecimal a = new BigDecimal(juli / 1000.0);
						double b = a.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
						distance = b + "km";
					} else {
						distance = juli + "m";
					}
					params.put("jobId", jobId);
					params.put("jobTitle", jobTitle);// 标题
					params.put("jobClasstype", jobClasstype);// 兼职类型(如1.体验2.互动3.分享4.校园5.个人)
					params.put("photo", image);// 兼职类型(如1.体验2.互动3.分享4.校园5.个人)所对应图片
					params.put("jobTypeType", jobTypeType);// 兼职类型下属分支(如促销)
					params.put("salaryUnit", salaryUnit1);// 薪资单位(如 元/日)
					params.put("jobSalary", jobSalary);// 薪资待遇
					params.put("settlementName", settlementName);// 薪资结算单位(如 日结)
					params.put("createTime", createTime);// 创建时间
					params.put("jobEndDate", jobEndDate);// 结束时间
					params.put("distance", distance);// 结束时间
					lsit5.add(params);
				}
				return lsit5;
			} else if (type.equals("1")) {
				sb.append("SELECT a.company_id,a.company_logo,a.company_name,a.company_character FROM phb_company a WHERE a.company_id in (SELECT company_id FROM phb_muser_collection  WHERE user_id="+mUserId1+" AND collection_type = 1)");
				if (!pageno.equals("") && !pageno.equals(null)) {
					int pageno1 = Integer.parseInt(pageno);
					sb.append(" LIMIT "+pageno1*30+","+(pageno1+1)*30);
				} else {
					sb.append(" LIMIT 0,30");
				}
				String sql = sb.toString();
				List<Map<String, Object>> lsit5 = new ArrayList();
				list1 = this.jdbcTemplate.queryForList(sql);
				for (Map<String, Object> map : list1) {
					Map<String, Object> params = new HashMap<String, Object>();
					int companyId = (int) map.get("company_id");//企业唯一标识
					String companyLogo = (String) map.get("company_logo");//企业logo
					String companyName = (String) map.get("company_name");//企业名称
					String companyCharacter = (String) map.get("company_character");//所属行业
					
					params.put("companyId", companyId);//企业唯一标识
					params.put("companyLogo", companyLogo);// 企业logo
					params.put("companyName", companyName);// 企业名称
					params.put("companyCharacter", companyCharacter);// 所属行业
					lsit5.add(params);
				}
				return lsit5;
			}
		}
		return null;
	}	
}
